package de.au.fi.imp.reader;
import java.util.HashMap;

import org.openqa.selenium.By;

public class MagicFormulaReader extends WebReader {
	
	public MagicFormulaReader()
	{
		getFirefoxDriver();
		driver.get("https://www.magicformulainvesting.com");
		wait(1);
	}

	public HashMap<String, String> getMagicStocks()
	{
		try {
			login();
		} catch (Exception e) {
			log.error("error logging in",e);
			return null;
		}
		try {
			getStocks();
		} catch (Exception e) {
			log.error("error getting stocks in page in",e);
			return null;
		}
		HashMap<String, String> magicStocks = null;
		try {
			magicStocks = getStockData();
		} catch (Exception e) {
			log.error("error error reading magic stocks on page",e);
		}
		try {
			logout();
		} catch (Exception e) {
			log.error("error logging out",e);
		}
		if(magicStocks == null || magicStocks.size() < 29)
			log.error("error getting magic tickers could only get {} tickers", magicStocks==null?"0":magicStocks.size()+"");
		return magicStocks;
	}
	
	private void login() throws Exception
	{
		setElementTo(By.xpath("/html/body/div/div/div[2]/div/ul[2]/li[3]/a"));
		click();
		setElementTo(By.xpath("//*[@id=\"email\"]"));
		element.sendKeys("goodfellas@gmx.li");
		setElementTo(By.xpath("//*[@id=\"passwd\"]"));
		element.sendKeys("palladium");
		setElementTo(By.xpath("/html/body/div/div[4]/div[11]/div/div/div[2]/div/div/div/div/div/div/div/div[2]/form/table/tbody/tr[4]/td[2]/div/input"));
		click();
		wait(3);
	}
	
	private void getStocks() throws Exception
	{
		setElementTo(By.xpath("//*[@id=\"numberStocks\"]"));
		element.setSelected();
		element.click();
		click();
		setElementTo(By.xpath("/html/body/div/div[4]/div/div[2]/div/div[2]/div/div/div/div/div/div/div/form/table/tbody/tr[4]/td[2]/div/input"));
		click();
		wait(5);
	}
	
	private HashMap<String, String> getStockData() throws Exception
	{
		HashMap<String, String> result = new HashMap<String, String>();
		int i = 1;
		String xpath;
		for(i = 1; i <= 50; i++)
		{
			if(i==1)
				xpath = "/html/body/div/div[4]/div/div[4]/div/div/div/div/div/div/div/div/div/table/tbody/tr/td";
			else
				xpath = "/html/body/div/div[4]/div/div[4]/div/div/div/div/div/div/div/div/div/table/tbody/tr["+i+"]/td";
			setElementTo(By.xpath(xpath));
			if(element == null && i > 30)
			{
				log.debug("Found 30 stocks returning");
				break;
			}
			else if(element == null && i < 30)
				throw new Exception("Could not find element by xpath "+xpath);
			String name = getName(xpath);
			String ticker = getTicker(xpath+"[2]");
			if(name != null && ticker != null)
				result.put(name, ticker);
			else return result;
		}
		return result;
	}
	
	private String getName(String xpath)
	{
		setElementTo(By.xpath(xpath));
		return logElementText("name");
	}
	
	private String getTicker(String xpath)
	{
		setElementTo(By.xpath(xpath));
		return logElementText("ticker");
	}
	
	private void logout()
	{
		setElementTo(By.xpath("/html/body/div/div/div[2]/div/ul/li[3]/a"));
		click();
		destroy();
	}
}

